using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
namespace IGlobalist.Cms
{
    public partial class Search : System.Web.UI.Page
    {
         protected void Page_Load(object sender, EventArgs e)
        {
             
            if (!Page.IsPostBack)
            {
                if (Request["tbSearch"]!= null)
                {
                   
                    String search = Request["tbSearch"];
                    String page = Request["hdnPage"];
                    String cat = Request["hdnCat"];
                    String content = Request["hdnContent"];
                    tbSearch.Text = search;
                    searchContent(search);
                    lbtnBack.PostBackUrl = String.Format("{0}_{1}_{2}/default.aspx", page, cat, content);
                }

                if (Request["advSearch"] == "1")
                {
                    string name = Request["tbName"];
                    string tags = Request["tbTags"];
                    string author = "" + Request["tbAuthor"];
                    string date = Request["tbDate"];
                    if (name == "") name = " ";
                    if (name != "" || tags != "" || author != "" || date != "")
                    {
                        Hashtable htSearch = new Hashtable();
                        htSearch["Name"] = "%" + name + "%";
                        htSearch["Tags"] = "%" + tags + "%";
                        if (date != null)
                            htSearch["CreatedDate"] = date;

                        VideoList listV = new VideoList();
                        listV = VideoController.GetVideoListBySearch(htSearch, author);
                        Session["VideoList"] = listV;
                        //Session["sVideo"] = htSearch;
                        string p = "default.aspx?search=1";
                        Response.Redirect(p);
                    }
                }

                if (Request["sVideo"]!=null)
                {
                    VideoList listV = new VideoList();
                    listV = VideoController.GetVideoListByWhere("%" + Request["sVideo"].ToString() + "%");
                    Session["VideoList"] = listV;
                    Session["sVideo"] = Request["sVideo"].ToString();
                    string p = "default.aspx?search=1";
                    Response.Redirect(p);
                }
            }
        }


        protected void searchContent(string search)
        {

            ContentList list = ContentController.GetContentListBySearch("%" + search +"%");
            IList<IContent> listContent = new List<IContent>();
            foreach (IContent content in list)
                listContent.Add(content);
            lblResult.Text = string.Format("{0} results for {1}", listContent.Count, search);
            gvContent.DataSource = list;
            gvContent.DataBind();

        }

        

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (tbSearch.Text.Trim() != "")
            {
                searchContent(tbSearch.Text);
            }
        }

       
    }
}
